package org.zebra.common.crypto;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;

import java.nio.charset.StandardCharsets;

/**
 * AES
 *
 * @author zhanghongbin
 */
public class AesCryptoProvider implements CryptoProvider {

    private String secretKey;

    public AesCryptoProvider(String secretKey) {
        this.secretKey = secretKey;
    }

    @Override
    public String encrypt(String value) {
        return SecureUtil.aes(secretKey.getBytes(StandardCharsets.UTF_8)).encryptBase64(value, StandardCharsets.UTF_8);
    }

    @Override
    public String decrypt(String value) {
        return SecureUtil.aes(secretKey.getBytes(StandardCharsets.UTF_8)).decryptStr(value, StandardCharsets.UTF_8);
    }

    public static final String generateSecretKey() {
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        return Base64.encode(key);
    }
}
